package com.wheesper.www.WheesperServer.Utils;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
//Logger Wrapper for Wheesper project
public class Logger {
	private File target;	//log file
	private static Logger logger;
	private Logger(String filePath) {
		target = new File(filePath);
		try {
			if(!target.createNewFile()) {
				System.out.printf("Log File -Found at %s\n",target.getAbsolutePath());
			}else {
				System.out.printf("Log File -Not Found-"
						+ "Auto Created at %s\n",target.getAbsolutePath());
			}
		}catch(IOException e) {
			e.printStackTrace();
		}
	}
	
	//formatting the log_info
	private String createLogInfo(String log,String separator) {
		String log_info = "";
		SimpleDateFormat dtf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		log_info = String.format("[Logging-%s-%s]-%s", 
				dtf.format(new Date()),
				log,
				separator);
		return log_info;
	}
	
	public static Logger Instance() {
		if(logger == null) {
			return (logger = new Logger(Config.Instance().getLoggerFilePath()));
		}
		return logger;
	}
	
	public void logging(String log) {
		System.out.println("");
		System.out.println(new Date());
		System.out.println(log);
		try(FileWriter output = new FileWriter(target,true)){
			output.write(createLogInfo(log,"\t\n"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		Logger.Instance().logging("Fine!");
	}
}
